/*
 * 描述
 * Created by: litangyu
 * 包含类名列表
 * 版本信息，版本号 v1.0
 * 创建日期 16/8/31
 * 版权声明
*/
package com.foton.logistic.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.foton.logistic.R;
import com.foton.logistic.base.BaseActivity;
import com.foton.logistic.listener.IOnResultListener;
import com.foton.logistic.model.ResultEntity;
import com.foton.logistic.task.DispatchTask;
import com.foton.logistic.task.ShowDialogTask;
import com.foton.logistic.task.TaskConstant;
import com.foton.logistic.util.BaseConstant;
import com.foton.logistic.util.OptionUtil;
import com.foton.logistic.util.net.HttpUtil;
import com.foton.logistic.util.tool.StringUtil;

import java.util.Map;

import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 描述
 * 修改密码
 * Created by: litangyu<br/>
 * Created on: 16/8/31 上午8:56
 */
public class ModifyPasswordActivity extends BaseActivity {

    @InjectView(R.id.ft_ui_et_old_password)
    public EditText mOrdPassword;

    @InjectView(R.id.ft_ui_et_new_password)
    public EditText mNewPassword;

    @InjectView(R.id.ft_ui_et_confirm_password)
    public EditText mConfirmPassWord;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }
        setTaskTag(getClass().getSimpleName());
        setContentView(R.layout.ft_ui_modify_password);
    }

    @Override
    public void init() {
        setBackLayoutVisibility(View.VISIBLE);
        setTitleText(getString(R.string.modify_password));
        setTitleTextVisibility(View.VISIBLE);

        showSoftInput(mOrdPassword);
    }

    @OnClick(R.id.ft_ui_confirm)
    public void confirm() {
        String oldPassword = mOrdPassword.getText().toString().trim();
        String newPassword = mNewPassword.getText().toString().trim();

        if (StringUtil.isEmpty(oldPassword)) {
            OptionUtil.addToast(this, getString(R.string.old_password_is_null));
            return;
        }

        if (StringUtil.isEmpty(newPassword)) {
            OptionUtil.addToast(this, getString(R.string.new_password_is_null));
        } else {
            if (oldPassword.equals(newPassword)) {
                OptionUtil.addToast(this, getString(R.string.confirm_old_and_new_password));
                return;
            } else {
                if (!StringUtil.equals(newPassword, mConfirmPassWord.getText().toString().trim())) {
                    OptionUtil.addToast(this, getString(R.string.confirm_two_password));
                } else {
                    request(oldPassword, newPassword);
                }
            }
        }
    }

    private void request(String oldPassword, String newPassword) {
        hideSoftInput(mOrdPassword);
        hideSoftInput(mNewPassword);
        hideSoftInput(mConfirmPassWord);

        Map<String, Object> argMap = HttpUtil.getEncryMap(this);
        argMap.put("oldPassword", oldPassword);
        argMap.put("newPassword", newPassword);

        ShowDialogTask task = new ShowDialogTask(this, taskTag, titleText,
                getString(R.string.committing), true,
                BaseConstant.MODIFY_PASSWORD, argMap, TaskConstant.POST);
        task.setParseClass(ResultEntity.class);
        task.setiOnResultListener(new IOnResultListener() {
            @Override
            public void onOK(DispatchTask task) {
                OptionUtil.addToast(ModifyPasswordActivity.this, getString(R.string
                        .update_password_success));
                finishSelf();
            }

            @Override
            public void onError(DispatchTask task) {
            }

            @Override
            public void onDone(DispatchTask task) {

            }
        });
        task.execute();
    }

}
